package org.tyut.xyl.game;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.liugang.tugofwar.R;

import org.tyut.xyl.game.util.GsonUtil;
import org.tyut.xyl.game.util.Information;
import org.tyut.xyl.game.util.OkHttpUtil;

import java.io.IOException;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.Request;
import okhttp3.Response;

public class GameMainActivity extends AppCompatActivity {
    private TextView game_main_username;
    private TextView game_main_userid;
    private Button game_main_createroom;
    private Button game_main_joinroom;
    private Button history;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game_main);
        game_main_username = (TextView) findViewById(R.id.game_main_username);
        game_main_userid = (TextView) findViewById(R.id.game_main_userid);
        game_main_userid.setText(MainActivity.userid);
        game_main_username.setText(MainActivity.username);
        Button joinroom_bt = (Button) findViewById(R.id.joinRoom);

        Button history = (Button) findViewById(R.id.history);

        //点击加入房间
        joinroom_bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GameMainActivity.this, JoinRoomActivity.class);
                startActivity(intent);

            }
        });
        //点击历史战绩
        history.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GameMainActivity.this, MyHistoryActivity.class);
                startActivity(intent);

            }
        });

    }

    public void onClick(View view) {

        FormBody formBody = new FormBody.Builder()
                .add("username", MainActivity.username)
                .build();
        Request request = new Request.Builder().url(Information.URL + "createroom")
                .post(formBody).build();
        Call call = OkHttpUtil.getOkHttpClient().newCall(request);
        call.enqueue(new Callback() {
                         @Override
                         public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                             Map<String, String> map = GsonUtil.getGsonBuilder().create().fromJson(response.body().string(), Map.class);
                             MainActivity.roommaster = "1";
                             MainActivity.roomid = map.get("roomid");
                             MainActivity.roomcount = "1";
                             MainActivity.team = "red";
                             Intent intent = new Intent(GameMainActivity.this, RoomActivity.class);
                             startActivity(intent);
                         }

                         @Override
                         public void onFailure(@NonNull Call call, @NonNull IOException e) {

                         }
                     }
        );
    }
}
